<template>
  <div class="settings-container">
    <div class="app-container">
      <el-card>
        <!-- 新增角色按钮 -->
        <el-row style="height: 60px">
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            @click="addRoleFn"
            >新增角色</el-button
          >
        </el-row>

        <!-- 表格 -->
        <el-table :data="list">
          <el-table-column type="index" label="序号" width="120" />
          <el-table-column label="角色名称" width="240" prop="name" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="scope">
              <el-button size="small" type="success">分配权限</el-button>
              <el-button size="small" type="primary" @click="edit(scope.row.id)"
                >编辑</el-button
              >
              <el-button
                size="small"
                type="danger"
                @click="deleteRoleFn(scope.row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row
          type="flex"
          justify="center"
          align="middle"
          style="height: 60px"
        >
          <!-- 分页组件 -->
          <!-- 
            分页逻辑: 页数 = 数据总条数 / 每页条数(10)
            :total = "总数"
            @current-change="" 点击是抛出当前点击的页数 作为接口参数去查找数据
           -->
          <el-pagination
            layout="prev,pager,next"
            :total="total"
            :page-size="params.pagesize"
            @current-change="pageChange"
          />
        </el-row>
      </el-card>
    </div>
    <!-- 新增弹框 -->
    <el-dialog title="编辑弹层" :visible="showDialog" @close="dialogClose">
      <el-form
        ref="roleForm"
        :model="roleForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small">取消</el-button>
          <el-button size="small" type="primary" @click="confirmFn"
            >确定</el-button
          >
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import {
  getRoleList,
  deleteRole,
  addRole,
  getRoleDetail,
  updateRole,
} from "@/api/setting";
export default {
  name: "setting",
  data() {
    return {
      list: [], // 角色列表
      params: {
        page: 1,
        pagesize: 3,
      },
      total: 0, // 总条数
      showDialog: false,
      roleForm: {
        name: "",
        description: "",
      },
      rules: {
        name: [
          { required: true, message: "角色名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    this.hGetRpleList();
  },
  methods: {
    async hGetRpleList() {
      const res = await getRoleList(this.params);
      this.list = res.data.rows;
      this.total = res.data.total;
    },
    // 页面发生改变时,自动调用
    pageChange(change) {
      // 分页器中点击哪页 table就显示哪页的具体数据
      // 1. 分页需要的页数 page
      // 2. 决定table渲染哪些数据 -> this.list
      this.params.page = change;
      this.hGetRpleList();
    },
    // 删除角色按钮
    deleteRoleFn(id) {
      this.$confirm("确认要删除吗", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          // 调用删除接口
          await deleteRole(id);
          // (删除成功后) 当我们再次发起请求的时候, 如果页面还有一条数据
          // 更新列表
          this.hGetRpleList();
          this.$message({
            type: "success",
            message: "删除角色成功!",
          });
        })
        .catch(() => {});
    },
    addRoleFn() {
      // 打开弹框
      this.showDialog = true;
    },
    dialogClose() {
      // 关闭弹框
      this.showDialog = false;
      // 初始化
      roleForm = {
        name: "",
        description: "",
      };
      // 清理校验痕迹
      this.$refs.roleForm.resetFields();
    },
    confirmFn() {
      // 兜底校验
      this.$refs.roleForm.validate(async (valid) => {
        if (valid) {
          // 调用新增接口
          await addRole(this.roleForm);
          // 1. 更新列表
          this.hGetRpleList();
          //  2. 关闭弹框
          this.dialogClose();
          // 3. 表单数据初始化
        }
      });
    },
    // 编辑按钮回调
    edit(id) {
      // 1. 展开弹框
      this.showDialog = true;
      // 2. 回填数据
      this.hGetRpleList(id);
    },
    // // 获取角色详情
    // async hGetRpleList(id) {
    //   const res = await getRoleDetail(id);
    //   // 回填操作
    //   this.roleForm = res;
    // },
  },
};
</script>

<style>
</style>